<template>
  <div class="container">
    <el-card class="card-style">
      <el-form ref="form" :model="form" :inline="true">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                v-model="form.keyword"
                style="width: 195px"
                placeholder="根据文章标题搜索"
                size="small"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="状态">
              <el-select v-model="form.state" placeholder="请选择" size="small">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
              <el-button
                size="small"
                style="margin-left: 10px"
                @click.native="clearText"
                >清除</el-button
              >
              <el-button type="primary" size="small" @click.native="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="success" size="small" @click.native="addActicle">
                <i class="el-icon-edit"></i>
                新增技巧
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 15px"
      ></el-alert>
      <!-- 表格要的是数组  数组对象  数组对象 数组对象  数组对象  不是对象!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
      <el-table :data="articleList" style="width: 100%" v-loading="loading">
        <el-table-column
          label="序号"
          width="80"
          type="index"
          :index="Changeindex"
        >
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="400">
          <template #default="{ row }">
            {{ row.title }}
            <a href="#" v-if="row.videoURL" @click="PalyVideo(row.videoURL)"
              ><i style="color: #00f; font-size: 18px" class="el-icon-film"></i
            ></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"> </el-table-column>
        <el-table-column prop="username" label="录入人"> </el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template #default="{ row }">
            {{ formatTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="80">
          <template #default="{ row }">
            {{ row.state ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" @click.native="LookDetails1(row)"
              >预览</el-button
            >
            <el-button
              type="text"
              v-if="!row.state"
              @click="ChangeState(row.id, row.state)"
              >启用</el-button
            >
            <el-button
              type="text"
              v-else
              @click="ChangeState(row.id, row.state)"
              >禁用</el-button
            >
            <el-button
              type="text"
              :disabled="row.state ? true : false"
              @click.native="editArticle1(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state ? true : false"
              @click.native="delArticle(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagFather">
        <el-pagination
          class="paging"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="  prev, pager, next,sizes, jumper"
          :total="counts"
          >>
        </el-pagination>
      </div>
    </el-card>
    <!-- 点击预览的弹框 -->
    <ArticlesPreview
      :look-dialog.sync="lookDialog"
      :user-row="userRow"
    ></ArticlesPreview>
    <!-- 点击添加或者修改的弹框 -->
    <ArticlesAdd
      :show-dialog.sync="showDialog"
      :user-row="userRow"
      ref="addArticle"
    ></ArticlesAdd>
    <div class="video-preview" v-if="Videolog">
      <div class="close" @click="closeVideo">
        <span class="el-icon-close"></span>
      </div>
      <div class="box">
        <video
          :src="videourl"
          width="100%"
          height="80%"
          autoplay
          controls
          loop
        ></video>
      </div>
    </div>
  </div>
</template>
<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import { parseTime } from '@/utils'
import ArticlesPreview from '../components/articles-preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
export default {
  components: {
    ArticlesPreview,
    ArticlesAdd
  },
  data () {
    return {
      loading: false, // loading效果
      page: 1, // 当前页
      pagesize: 10, // 页数量
      form: {
        keyword: '', // 关键字
        state: '' // 状态 1 开启 0 屏蔽
      },
      counts: 0, // 总条数
      articleList: [], // 保存列表数据
      lookDialog: false, // 预览的弹框
      showDialog: false, // 添加和修改的
      Videolog: false, // 视频弹框
      videourl: '',
      userRow: {} // 预览和回显使用的row
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 获取列表数据方法
    async getlist () {
      this.loading = true
      if (this.form.state) {
        const { data } = await list({
          page: this.page,
          pagesize: this.pagesize,
          keyword: this.form.keyword,
          state: this.form.state
        })
        this.articleList = data.items
        this.counts = data.counts
        this.loading = false
      } else {
        const { data } = await list({
          page: this.page,
          pagesize: this.pagesize,
          keyword: this.form.keyword
        })
        this.articleList = data.items
        this.counts = data.counts
        this.loading = false
      }
    },
    // 切换每页多少条执行的方法
    handleSizeChange (val) {
      // console.log(val)
      this.pagesize = val
      this.page = 1
      this.getlist()
    },
    // 切换当前页执行的方法
    handleCurrentChange (val) {
      // console.log(val)
      this.page = val
      // this.search()
      this.getlist()
    },
    // 页码计算
    Changeindex (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 点击清除时候的方法
    clearText () {
      this.form = { keyword: '', state: '' }
    },
    // 点击搜索的时候
    search () {
      this.getlist()
    },
    // 点击启用禁用的方法
    async ChangeState (id, state) {
      await changeState({ id, state: state ? 0 : 1 })
      this.$message.success('操作成功')
      this.getlist()
    },
    // 转换时间的方法
    formatTime (Time) {
      return parseTime(Time)
    },
    // 点击预览的方法
    LookDetails1 (row) {
      this.userRow = row
      this.lookDialog = true
    },
    // 点击添加的方法
    addActicle () {
      this.showDialog = true
    },
    // 点击修改的方法
    async editArticle1 (row) {
      this.showDialog = true
      // 做数据回显的方法
      this.userRow = row
      this.$nextTick(() => {
        this.$refs.addArticle.getArticleList()
      })
    },
    // 点击删除的方法
    delArticle (id) {
      this.$confirm('你确定要删除吗大侠?', '温馨提示', { type: 'warning' })
        .then(async () => {
          await remove({ id })
          this.$message.success('删除成功')
          if (this.articleList.length <= 1 && this.page > 1) {
            this.page--
          }
          this.getlist()
        })
        .catch(() => {})
    },
    // 播放视频的方法
    PalyVideo (url) {
      this.videourl = url
      this.Videolog = true
    },
    // 关闭视频弹层 并且关闭视频
    closeVideo () {
      this.videourl = ''
      this.Videolog = false
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  ::v-deep .el-table th {
    // background-color: pink;
    background-color: #fafafa;
    border-bottom: 2px solid #e8e8e8 !important;
  }
  .card-style {
    margin: 10px 10px 0;
  }
  .pagFather {
    margin-top: 21px;
    display: flex;
    justify-content: flex-end;
  }
  .video-preview {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
    .close {
      width: 50px;
      height: 50px;
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translate(-50%);
      background: rgba(0, 0, 0, 0.4);
      border-radius: 50%;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 20px;
      .el-icon-close:before {
        content: "\e6db";
      }
    }
    .box {
      width: 800px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 60%;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
  }
}
</style>
